package com.itguigu.auth.activiti;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  加班流程测试 - 全局变量
 */
@SpringBootTest
public class ProcessTestJiaban3 {

    @Autowired
    private RepositoryService repositoryService;


    /**
     * 流程定义部署
     */
    @Test
    public void deployProcess() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban.bpmn20.xml")
                .addClasspathResource("process/jiaban.png")
                .name("加班申请流程")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    @Autowired
    private RuntimeService runtimeService;

    /**
     * 开启流程实例
     */
    @Test
    public void startUpProcessInstance(){
        Map<String, Object> variables = new HashMap<>();
        variables.put("assignee1", "lusi02");
        // variables.put("assignee2", "mary02");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban", variables);
        System.out.println("processInstance.getProcessDefinitionId() = " + processInstance.getProcessDefinitionId());
        System.out.println("processInstance.getId() = " + processInstance.getId());
    }

    @Autowired
    private TaskService taskService;

    @Test
    public void findTaskList() {
        List<Task> list = taskService.createTaskQuery().taskAssignee("he").list();
        for (Task task : list) {
            System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
            System.out.println("task.getName() = " + task.getName());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
        }
    }

    @Test
    public void completTask() {
        Task task = taskService.createTaskQuery()
                .taskAssignee("lusi02")
                .singleResult();
        HashMap<String, Object> map = new HashMap<>();
        map.put("assignee2", "he");
        taskService.complete(task.getId() , map);
    }



}
